package com.customization.unionpay.market.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.util.FormUtil;
import com.customization.unionpay.market.service.CheckReimbursementWorkflowService;
import weaver.backup.logging.Logger;
import weaver.backup.logging.LoggerFactory;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CheckReimbursementWorkflowServiceImpl implements CheckReimbursementWorkflowService {

    public final Logger log = LoggerFactory.getLogger(CheckReimbursementWorkflowServiceImpl.class);

    /**
     * 根据活动授权码查询是否有
     * @param sqm 授权码集合
     * @return
     */
    @Override
    public List<JSONObject> getCheckReimbursementWorkflowBySqm(List<String> sqm) {
        StringBuilder querMainySql = new StringBuilder();
        querMainySql.append("select id,requestid,bdh,ywlx,sqr,bzgs,bzbm,ywrq,rzrq,bzsy,sfhtbz,")
                .append("fpzs,htbh,htmc,ckht,htbde,htlzje,htyfje,xmbh,xmmc,zdgzbm,gysbh,")
                .append("gysmc,gysdd,hdmc,yxpthdm,fjsc,hdmscrq,hdlx,hdxs,hxdj,yrzje,")
                .append("yhxje,zjly,sfjngs,hdhxlx,hdnbsyje,nwbczbl,fplx,zdgzmc from ")
                .append(com.customization.unionpay.market.util.FormUtil.CHECK_REIMBURSEMENT)
                .append(" where yxpthdm in (");
        sqm.forEach(o->{
            querMainySql.append("'").append(o).append("'").append(",");
        });
        querMainySql.deleteCharAt(querMainySql.length()-1).append(")");



        //这里查询核销报账单的信息
        RecordSet rs = new RecordSet();
        try {
            rs.execute(querMainySql.toString());
            String[] columns =  rs.getColumnName();
            List<JSONObject> result = new ArrayList<>();
            while (rs.next()){
                JSONObject jsonObject = new JSONObject();
                for (String column : columns) {
                    jsonObject.put(column,rs.getString(column));
                }
                result.add(jsonObject);
            }
            List<Integer> mainId = result.stream().map(o->o.getInteger("id")).collect(Collectors.toList());
            StringBuilder queryDt1Sql = new StringBuilder();
            queryDt1Sql.append("select id,mainid,jjsx from ").append(FormUtil.CHECK_REIMBURSEMENT+"_dt1 ")
                    .append(" where mainid in (");
            mainId.forEach(o->{
                queryDt1Sql.append(o).append(",");
            });
            queryDt1Sql.deleteCharAt(queryDt1Sql.length()-1).append(")");
            rs = new RecordSet();
            rs.execute(queryDt1Sql.toString());
            JSONArray dt1List = new JSONArray();
            columns = rs.getColumnName();
            Map<Integer,JSONArray> dt1Map = new HashMap<>();
            while (rs.next()){
                JSONObject jsonObject = new JSONObject();
                for (String column : columns) {
                    jsonObject.put(column,rs.getString(column));
                }
                    if(dt1Map.get(jsonObject.getInteger("mainid"))!=null){
                        dt1Map.get(jsonObject.getInteger("mainid")).add(jsonObject);
                    }else{
                        JSONArray array = new JSONArray();
                        array.add(jsonObject);
                        dt1Map.put(jsonObject.getInteger("mainid"),array);
                    }
                dt1List.add(jsonObject);
                }
            result.forEach(o->{
                o.put("dt1",dt1Map.get(o.getInteger("id")));
            });
            return result;
        }catch (Exception e) {
            log.error("查询核销报账单的数据时出错！"+e.getMessage());
        }
        return null;
    }
}


